using System;
using System.Collections;
using System.Collections.Generic;

namespace OF.Security.Utils
{
	public abstract class ContextCacheQueueBase
	{
	}
	public abstract class ContextCacheQueueBase<TKey, TValue> : ContextCacheQueueBase
	{
		public delegate TValue ContextCacheItemNotExistsAction(ContextCacheQueueBase<TKey, TValue> cache, TKey key);

		private Dictionary<TKey, TValue> innerDictionary = new Dictionary<TKey, TValue>();

		public TValue this[TKey key]
		{
			get
			{
				return this.innerDictionary[key];
			}
			set
			{
				this.innerDictionary[key] = value;
			}
		}

		public int Count
		{
			get
			{
				return this.innerDictionary.Count;
			}
		}

		public TValue Add(TKey key, TValue data)
		{
			this.innerDictionary[key] = data;
			return data;
		}

		public bool TryGetValue(TKey key, out TValue data)
		{
			return this.innerDictionary.TryGetValue(key, out data);
		}

		public TValue GetOrAddNewValue(TKey key, ContextCacheQueueBase<TKey, TValue>.ContextCacheItemNotExistsAction action)
		{
			TValue result = default(TValue);
			if (!this.TryGetValue(key, out result))
			{
				result = action(this, key);
			}
			return result;
		}

		public bool Remove(TKey key)
		{
			return this.innerDictionary.Remove(key);
		}

		public void Clear()
		{
			this.innerDictionary.Clear();
		}

		public bool ContainsKey(TKey key)
		{
			return this.innerDictionary.ContainsKey(key);
		}

		public IEnumerator GetEnumerator()
		{
			foreach (KeyValuePair<TKey, TValue> current in this.innerDictionary)
			{
				yield return current;
			}
			yield break;
		}
	}
}
